<template>
  <div>
    <el-form
      :model="updatePwd"
      status-icon
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="新密码" prop="pwd">
        <el-input
          type="password"
          placeholder="请输入密码"
          v-model="updatePwd.pwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newpwd">
        <el-input
          type="password"
          placeholder="请确认密码"
          v-model="updatePwd.newpwd"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive } from '@vue/reactivity'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { http } from '../../utils/http'
const store = useStore()
const router = useRouter()
const updatePwd = reactive({
  _id:store.state.msg._id,
  pwd:'',
  newpwd:''
})
const submitForm = async () => {
  console.log(updatePwd);
  if(!updatePwd.pwd || (updatePwd.pwd !== updatePwd.newpwd)){
    ElMessage.error({
      type:'error',
      message:'修改失败'
    })
    return
  }
  const res =await http.post('/user/updatePwd', updatePwd)
  ElMessage.success({
      type:'success',
      message:'修改成功'
    })
  console.log(res);
  localStorage.removeItem('vuex')
  localStorage.removeItem('token')
  router.push({'name': "Login"})

}
</script>

<style lang="scss" scoped>
.el-form{
  width: 30%;
  margin: 50px auto 0;
  div:last-child{
    div{
      button{
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      }
    }
    
  }
}
</style>